package com.lvyq.myplayer.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.parcelable.SongInfoData
import com.squareup.picasso.Picasso
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation

/**
 * 文件描述：IndexZaiTingAdapter 首页，大家在听
 * 作者：lvyq
 * 创建时间：2025-03-26
 * 修改时间：2025-03-26
 */
class IndexHotSongAdapter(var context:Context?, var listData: List<SongInfoData> ) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


    // 定义点击事件接口
    interface OnItemClickListener {
        fun onItemClick(data: SongInfoData, position: Int)
    }

    private var onItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var view = View.inflate(context, R.layout.item_index_zaiting, null)

        //设置item宽度为屏幕的1/2- 5*2
        // 使用 ViewTreeObserver 确保在视图附加后执行
        view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                view.viewTreeObserver.removeOnGlobalLayoutListener(this)

                val screenWidth = context?.resources?.displayMetrics?.widthPixels
                val cellWidth = screenWidth?:900
                // 安全更新布局参数
                (view.layoutParams as? RecyclerView.LayoutParams)?.let { params ->
                    //调整宽度为屏幕的3/5
                    params.width = cellWidth*3/5
                    view.layoutParams = params
                }
            }
        })

        return indexZaiTingHolder(view)
    }

    override fun getItemCount(): Int {
        return listData.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        var data = listData.get(position)
        (holder as indexZaiTingHolder).setDate(data)
        //添加点击事件
        holder.itemView.setOnClickListener{
            onItemClickListener?.onItemClick(data,position)
        }
    }

    inner class  indexZaiTingHolder(item:View):RecyclerView.ViewHolder(item){

        val  bgImg:ImageView
        val  title:TextView
        val  singer:TextView
        val  des:TextView

        init {
            bgImg=item.findViewById(R.id.bgImg)
            title=item.findViewById(R.id.title)
            singer=item.findViewById(R.id.singer)
            des=item.findViewById(R.id.des)
        }
        fun setDate(data: SongInfoData){
            Picasso.with(context).load(data.bgImg).transform(RoundedCornersTransformation(30,0)).into(bgImg)
            title.text=data.title
            singer.text="-"+data.singer
            des.text="超过"+data.heat+"%人播放"
        }
    }

}